//
//  ProjectListViewController.m
//  mhome
//
//  Created by YaSha_Tom on 2018/12/6.
//  Copyright © 2018年 刘春雷. All rights reserved.
//

#import "ProjectListViewController.h"
#import "ProjectInfoViewController.h"
#import "YSPMSInfoListCell.h"
#import "YSNetManager.h"
#import "DataManager.h"
#import "Tools.h"

@interface ProjectListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableDictionary *payload;
@property (nonatomic,copy) NSArray *dataSourceArray;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation ProjectListViewController
- (NSMutableDictionary *)payload {
    if(!_payload){
        _payload = [NSMutableDictionary dictionary];
    }
    return _payload;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的项目";
    [self initUI];
    [self doNetworking];
}
- (void)initUI {
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (void)doNetworking {
    NSString *urlString = [NSString stringWithFormat:@"%@%@", YSDomain, getHeader];
    NSDictionary *dic = @{
                          @"encodeContext": @"I/NfSPpOtY+KrQgcd7291TiRrvER3ehOY2BkT21ACVk0/y4NiWfmVLRB4zFVi988ptHseJLHXvT6waK/ohsjPqJ6L/7lQyNPoE8Bshk7kJc="
                          };
    [YSNetManager ys_request_POSTWithUrlString:urlString isNeedCache:NO parameters:dic successBlock:^(id response) {
        NSLog(@"成功%@",response);
        if([response[@"code"]  isEqual: @1]){
            YSNetManagerShare.httpHeaderFieldDictionary = [Tools getHTTPHeaderFieldDictionary:response[@"data"][@"jwtstr"]];
            NSLog(@"-----%@",[Tools getHTTPHeaderFieldDictionary:response[@"data"][@"jwtstr"]]);
            [self getProjectlist];
        }else{
            [QMUITips showInfo:@"认证出错" inView:self.view hideAfterDelay:1];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"失败%@",error);
    } progress:nil];
}

- (void)getProjectlist {
    NSString *urlString = [NSString stringWithFormat:@"%@%@/0",YSDomain, getBaseInfoListApp];
    [self.payload setValue:@"0001K310000000000ABV" forKey:@"priCompanyId"];
    [self.payload setValue:@"zyxm" forKey:@"proNature"];
    [self.payload setValue:@"" forKey:@"auditStatusStr"];
    [self.payload setValue:@"true" forKey:@"isAllPriCompany"];
    [YSNetManager ys_request_GETWithUrlString:urlString isNeedCache:NO parameters:self.payload successBlock:^(id response) {
        NSLog(@"成功%@",response);
        if([response[@"code"]  isEqual: @1]){
            self.dataSourceArray =[DataManager getPMSInfoListData:response];
            [self.tableView reloadData];
        }else {
            [QMUITips showInfo:response[@"msg"] inView:self.view hideAfterDelay:1];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"失败%@",error);
    } progress:nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YSPMSInfoListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[YSPMSInfoListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    ProjectListModel *cellModel = self.dataSourceArray[2];
    [cell setCellModel:cellModel];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
    ProjectListModel *model = self.dataSourceArray[2];
    ProjectInfoViewController *PMSInfoViewController = [[ProjectInfoViewController alloc] init];
    PMSInfoViewController.projectId = model.id;
    [self.navigationController pushViewController:PMSInfoViewController animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 97*kHeightScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
